package servlets;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.PositionService;
import entityJPA.Position;

/**
 * Servlet implementation class PositionsListServlet
 */
@WebServlet("/PositionsListServlet")
public class PositionsListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	private PositionService ps;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PositionsListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Vector<Position> list = this.getOpenPositions();
		HttpSession h= request.getSession();
		h.setAttribute("list", list);
		response.sendRedirect("index.jsp");
	}

	public Vector<Position> getOpenPositions(){
		try {
			return ps.getActivePositions();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
